package com.study.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.modules.sys.mapper.SysRoleMapper;
import com.study.modules.sys.mapper.SysUserRoleMapper;
import com.study.modules.sys.model.entity.SysRole;
import com.study.modules.sys.model.entity.SysUserRole;
import com.study.modules.sys.service.SysRoleMenuService;
import com.study.modules.sys.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author Mr.Du
 * @Date 2021/10/16 10:49
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private SysRoleMenuService roleMenuService;
    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public Set<String> selectUserRole(Long userId) {
        //根据用户查询角字符色列表
        List<Long> roleIdList = userRoleMapper.selectRoleIdListByUserId(userId);
        Set<String> permissionsList = new HashSet<>();
        for (Long roleId : roleIdList) {
            Set<String> permsList = roleMenuService.selectMenuByRoleId(roleId);
            permissionsList.addAll(permsList);
        }
        return permissionsList;
    }

    @Override
    public List<SysRole> selectRoleListByUserId(Long userId) {
        List<Long> roleIdList = userRoleMapper.selectRoleIdListByUserId(userId);
        List<SysRole> roleList = new ArrayList<>();
        if (roleIdList != null && roleIdList.size() > 0) {
            for (Long roleId : roleIdList) {
                SysRole role = roleMapper.selectRoleById(roleId);
                if (role != null){
                    roleList.add(role);
                }
            }
        }
        return roleList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertOrUpdate(Long userId, List<Long> roleIdList) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        this.removeByMap(map);
        if (roleIdList == null || roleIdList.size() == 0) {
            return true;
        }
        for (Long roleId : roleIdList) {
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            this.save(userRole);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteUserRoleByRoleId(Long roleId) {
        List<Long> list = baseMapper.selectUserIdListByRoleId(roleId);
//        if (list != null || list.size() > 0) throw new ApiException("角色存在绑定的用户，请为用户解除绑定！");
        if (list != null && list.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("role_id", roleId);
            baseMapper.deleteByMap(map);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteUserRoleByUserId(Long userId) {
        List<Long> list = baseMapper.selectRoleIdListByUserId(userId);
        if (list != null && list.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("user_id", userId);
            baseMapper.deleteByMap(map);
        }
        return true;
    }

}
